package com.interagriculture.service.impl;

import com.github.pagehelper.PageHelper;
import com.interagriculture.common.Constants;
import com.interagriculture.common.CustomException;
import com.interagriculture.common.FilePathConstants;
import com.interagriculture.common.PageResult;
import com.interagriculture.entity.UserInfo;
import com.interagriculture.enums.ExceptionCodeEnum;
import com.interagriculture.enums.PictureTypeEnum;
import com.interagriculture.mapper.UserInfoMapper;
import com.interagriculture.entity.query.UserInfoQuery;
import com.interagriculture.service.UserInfoService;
import com.interagriculture.utils.CacheContainerUtil;
import com.interagriculture.utils.UploadFileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * (UserInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-10-23 17:20:21
 */
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
    @Value("file.upload.path")
    private String filePath;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UploadFileUtil uploadFileUtil;

    /**
     * 分页查询学生
     * @param userInfoQuery
     * @return
     */
    @Override
    public PageResult selectByPage(UserInfoQuery userInfoQuery) {
        UserInfo userInfo = new UserInfo();
        if(userInfoQuery!=null&&userInfoQuery.getPageNum()!=null && userInfoQuery.getPageSize()!=null){
            PageHelper.startPage(userInfoQuery.getPageNum(),userInfoQuery.getPageSize());
        }
        //默认排序  id desc
        if(userInfoQuery!=null && userInfoQuery.getOrderBy()!=null){

            PageHelper.orderBy(userInfoQuery.getOrderBy());
        }else {
            PageHelper.orderBy("id desc");
        }
        if(userInfoQuery!=null){
            BeanUtils.copyProperties(userInfoQuery,userInfo);
        }
        List<UserInfo> userInfoList = userInfoMapper.selectByPage(userInfoQuery);

        long count = userInfoMapper.count(userInfo);
        for(UserInfo userInf:userInfoList){
            userInf.setStudentId(null);
        }
        return new PageResult(count,userInfoList);
    }

    /**
     * 修改学生信息
     * @param userInfo
     */
    @Override
    @Transactional
    public void saveOrUpdateInfo(UserInfo userInfo) {
        //id为空，则为添加信息
        //添加后返回id
        //如果是添加，先添加基本信息，
        //添加操作
        if(userInfo.getId() == null){
            // 是添加操作
            userInfoMapper.insertUserInfo(userInfo);
        }else{
            // 是修改操作
            userInfoMapper.updateUserInfo(userInfo);
        }

    }

    /**
     * 根据id查询学生
     * @param id
     * @return
     */
    @Override
    public UserInfo selectById(Integer id) {
        UserInfo userInfo = userInfoMapper.queryById(id);
        if(userInfo!=null){
            return userInfo;
        }
     throw new CustomException(ExceptionCodeEnum.USER_NOT_EXIST);

    }

    /**
     * 根据id删除学生
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        userInfoMapper.deleteById(id);
    }
}
